package pool;

import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  

/**
 * 
 * 这个可以通过JConsole进行分析，自始至终都只有一个新建的线程，即 pool-1-thread-1
 * CachedThreadPoolExecutor实际上创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程
 * 若无可回收，则新建线程。示例代码如下：
 * @author lcx
 *
 */
public class CachedThreadPoolExecutorSingleTest {  
	public static void main(String[] args) {  

		ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
		for (int i = 0; i < 100; i++) {  
			final int index = i;  
			try {  
				Thread.sleep(index * 10);  
			} catch (InterruptedException e) {  
				e.printStackTrace();  
			}  
			cachedThreadPool.execute(new Runnable() {  
				public void run() {  
					System.out.println(index);  
				}  
			});  
		}  
	}  
}  